import cv2 as cv
from python_ai.common.xcommon import *
import numpy as np
import os
import sys
import datetime
import matplotlib.pyplot as plt

spr = 1
spc = 3
spn = 0
plt.figure(figsize=[12, 6])


def show_img(title, img, trans=None, **kwargs):
    global spn
    spn += 1
    if spn > spr * spc:
        return
    plt.subplot(spr, spc, spn)
    plt.axis('off')
    if trans is not None:
        img = trans(img)
    plt.imshow(img, **kwargs)


# load
img = cv.imread('../../../../large_data/CV2/lesson/Day03/4perspective03.jpg', cv.IMREAD_COLOR)
img_shape = img.shape
IMG_H = img_shape[0]
IMG_W = img_shape[1]
show_img('ori', img, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

# tick
THICK = 50
pt1 = (349, 81)
pt2 = (2289, 165)
pt3 = (117, 1161)
pt4 = (2109, 1369)
cv.circle(img, pt1, THICK, (0, 255, 0), -1)
cv.circle(img, pt2, THICK, (0, 255, 0), -1)
cv.circle(img, pt3, THICK, (0, 255, 0), -1)
cv.circle(img, pt4, THICK, (0, 255, 0), -1)
show_img('ticked', img, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

# perspective trans
pt1a = (0, 0)
pt2a = (IMG_W, 0)
pt3a = (0, IMG_H)
pt4a = (IMG_W, IMG_H)
pts1 = np.float32((pt1, pt2, pt3, pt4))
pts2 = np.float32((pt1a, pt2a, pt3a, pt4a))
M = cv.getPerspectiveTransform(pts1, pts2)
img2 = cv.warpPerspective(img, M, (IMG_W, IMG_H))  # ATTENTION warp
show_img('perspective trans', img2, lambda x: cv.cvtColor(x, cv.COLOR_BGR2RGB))

plt.show()
